/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.jx.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.junit.runners.Parameterized.Parameters;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.PageLayUI;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.food.entity.FoodIncome;
import com.thinkgem.jeesite.modules.food.entity.FoodInfo;
import com.thinkgem.jeesite.modules.food.entity.FoodInfoSupplier;
import com.thinkgem.jeesite.modules.food.entity.FoodInfoSupplierSublist;
import com.thinkgem.jeesite.modules.food.entity.FoodMarketResearch;
import com.thinkgem.jeesite.modules.food.entity.FoodMarketResearchSublist;
import com.thinkgem.jeesite.modules.food.entity.FoodSupplier;
import com.thinkgem.jeesite.modules.food.entity.statistics.FoodOrderFormSummary;
import com.thinkgem.jeesite.modules.jx.entity.ProjectPerformanceReport;
import com.thinkgem.jeesite.modules.jx.service.ProjectPerformanceReportService;
import com.thinkgem.jeesite.modules.qc.entity.Assets;
import com.thinkgem.jeesite.modules.qc.entity.InvestmentDecisions;
import com.thinkgem.jeesite.modules.qc.entity.SeniorAssets;
import com.thinkgem.jeesite.modules.qc.service.AssetsService;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.test.entity.ReturnData;
//import com.thinkgem.jeesite.modules.qc.entity.WarntimeData;

/**
 * 绩效考核	Controller
 * @author 
 * @version 2022-07
 * @param <ProjectPerformanceReportList>
 */
@Controller
@RequestMapping(value = "${adminPath}/jx/projectPerformanceReport")
public class ProjectPerformanceReportController<ProjectPerformanceReportList>  extends BaseController {

	private static final List<ProjectPerformanceReport> projectPerformanceReportList = null;
	@Autowired
	private ProjectPerformanceReportService projectPerformanceReportService;
	private Object object;
	private String str;
	@Autowired
	private   OfficeService officeService ;
	
	@ModelAttribute
	public ProjectPerformanceReport get(@RequestParam(required=false) String id) {
		ProjectPerformanceReport entity = null;
		if (StringUtils.isNotBlank(id)){
			entity =projectPerformanceReportService.get(id);
		}
		if (entity == null){
			entity = new ProjectPerformanceReport();
		}
		return entity;
	}
	@RequestMapping(value = {"list", ""})
	public String list(ProjectPerformanceReport projectPerformanceReport, HttpServletRequest request, HttpServletResponse response, Model model) {
//		Page<Assets> page = AssetsService.findPage(new Page<Assets>(request, response), assets); 
		Page<ProjectPerformanceReport> page = projectPerformanceReportService.findPage(new Page<ProjectPerformanceReport>(request, response), projectPerformanceReport);
		
		model.addAttribute("page", page);
		model.addAttribute("ProjectPerformanceReport",projectPerformanceReport);
		return "modules/jx/projectPerformanceReportList";
	}
	@ResponseBody
	@RequestMapping(value = {"listByAjax"})
	public String listByAjax(HttpServletRequest request, HttpServletResponse response, Model model)throws ParseException {
		String year = request.getParameter("year");
		String updateDate = request.getParameter("updateDate");
		String fillInDate = request.getParameter("fillInDate");
		String approvalDate = request.getParameter("approvalDate");
		String officeId = request.getParameter("officeId");//时间查询
		
		String incomeDateStr = request.getParameter("incomeDateStr");
		Office office = new Office();
		office.setId(officeId);
	/*	FoodIncome foodIncome = new FoodIncome();
		foodIncome.setOffice(office);*/
		ProjectPerformanceReport projectPerformanceReport = new ProjectPerformanceReport();
		if(fillInDate != null && !fillInDate.equals("") && approvalDate != null && !approvalDate.equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			projectPerformanceReport.setFillInDate(sdf.parse(fillInDate));
			projectPerformanceReport.setApprovalDate(sdf.parse(approvalDate));
			
			
		} 
		
		List<ProjectPerformanceReport> projectPerformanceReportsList = projectPerformanceReportService.findList(projectPerformanceReport);//数据库接收的集合
	
	 
		for(int i=0;i<projectPerformanceReportsList.size();i++){
			ProjectPerformanceReport projectPerformanceReport2 = projectPerformanceReportsList.get(i);
			projectPerformanceReport2.setYear(DictUtils.getDictLabel(projectPerformanceReport2.getYear(), "jx_projectPerformanceReport_year", ""));
			projectPerformanceReportsList.set(i, projectPerformanceReport2);
		}		 
		ReturnData returnData = new ReturnData();
		returnData.setCode("0");
		returnData.setCount(String.valueOf(projectPerformanceReportsList.size()));
		returnData.setData(projectPerformanceReportsList);
		String jsonData = JSON.toJSONString(returnData);
		System.out.println(jsonData);
		return jsonData;
	}
	@RequestMapping(value = "form")
	public String form(ProjectPerformanceReport projectPerformanceReport, Model model) {
		model.addAttribute("ProjectPerformanceReport", projectPerformanceReport);
		return "modules/jx/projectPerformanceReportForm";
	}
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(String dataString, Model model, RedirectAttributes redirectAttributes) {
		dataString = StringEscapeUtils.unescapeHtml4(dataString);
		
		/*ProjectPerformanceReport projectPerformanceReport = UserUtils.getUser().getOffice();*/
		ProjectPerformanceReport projectPerformanceReport = JSON.parseObject(dataString,ProjectPerformanceReport.class);
		projectPerformanceReport.setOffice(UserUtils.getUser().getOffice());//office定义赋值。
		projectPerformanceReportService.save(projectPerformanceReport);
		String status =  JSON.toJSONString("保存数据成功");
		return status;
	}
	/**
	 * 导出用户数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "btnexport", method=RequestMethod.POST)
    public String export(ProjectPerformanceReport projectPerformanceReport, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String export = "绩效考核数据"+DateUtils.getDate("yyyyMMddHH")+".xlsx";
//        	 List<Assets> list = assetsService.findList(assets);
            List<ProjectPerformanceReport> list = projectPerformanceReportService.findList(projectPerformanceReport);
     		new ExportExcel("绩效考核数据", ProjectPerformanceReport.class).setDataList(list).write(response, export).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出用户失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/jx/projectPerformanceReport/list?repage";
    }
	@ResponseBody
	@RequestMapping(value = "deleteById")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		ProjectPerformanceReport projectPerformanceReport = new ProjectPerformanceReport();
		projectPerformanceReport.setId(id);
		projectPerformanceReportService.delete(projectPerformanceReport);
		String status =  JSON.toJSONString("删除成功");
		return status;
	}
	

}



